package com.foreknow.oop.hw7;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        Book[] books = test.initial();
        Orders orders = new Orders();
        OrdersDetail[] ordersDetails = test.inputOrders(books);
        test.setOrdersTotal(orders,ordersDetails);
        test.showOrdersInfo(orders,ordersDetails);
    }

    /**
     * 初始化图书
     * @return
     */
    public Book[] initial() {
        Book b1 = new Book(1, "java", 33.45, 50);
        Book b2 = new Book(2, "jsp", 55.45, 150);
        Book b3 = new Book(3, "ssm", 77.45, 250);
        Book[] books = new Book[3];
        books[0] = b1;
        books[1] = b2;
        books[2] = b3;
        return books;
    }

    /**
     * 购买图书
     * @param books 图书对象数组
     * @return
     */
    public OrdersDetail[] inputOrders(Book[] books){
        showBooks(books);
        Scanner sc = new Scanner(System.in);
        OrdersDetail[] ordersDetails = new OrdersDetail[3];
        for (int i = 3,j = 0; i > 0; i--,j++) {
            OrdersDetail ordersDetail = new OrdersDetail();
            System.out.print("请输入图书的编号：");
            int bookId = sc.nextInt();
            Book bookInfo = getBookInfo(bookId, books);
            if (bookInfo == null){
                System.out.println("无该编号的图书，请重新输入！");
                i++;
                j--;
                continue;
            }
            System.out.print("请输入购买数量：");
            int number = sc.nextInt();
            if (number > bookInfo.getInventory()){
                System.out.println("购买数量超出库存！");
                i++;
                j--;
                continue;
            }
            ordersDetail.setBookName(bookInfo.getBookName());
            ordersDetail.setBookPrice(bookInfo.getBookPrice());
            ordersDetail.setNumber(number);
            ordersDetails[j] = ordersDetail;
            System.out.println("请继续购买图书");
        }
        return ordersDetails;
    }

    /**
     * 根据图书编号获得图书信息
     * @param bookId 图书编号
     * @param books 图书对象数组
     * @return
     */
    public Book getBookInfo(int bookId,Book[] books){
        for (Book book :
                books) {
            if (book.getBookId() == bookId){
                return book;
            }
        }
        return null;
    }

    /**
     * 计算订单总额
     * @param orders 订单
     * @param ordersDetails 订单详情
     */
    public void setOrdersTotal(Orders orders,OrdersDetail[] ordersDetails){
        for (OrdersDetail ordersDetail :
                ordersDetails) {
            double ordersTotal = orders.getOrdersTotal();
            orders.setOrdersTotal(ordersTotal + ordersDetail.getBookPrice() * ordersDetail.getNumber());
        }

    }

    /**
     * 输出所有图书信息
     * @param books 图书对象数组
     */
    public void showBooks(Book[] books){
        for (Book book : books) {
            System.out.printf("%d\t%s\t%.2f\t%d\n",book.getBookId(),book.getBookName(),book.getBookPrice(),book.getInventory());
        }
    }

    /**
     * 输出订单信息
     * @param orders 订单
     * @param ordersDetails 订单详情
     */
    public void showOrdersInfo(Orders orders,OrdersDetail[] ordersDetails){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入订单编号：");
        int ordersId = sc.nextInt();
        System.out.printf("订单的总额为：%f\n",orders.getOrdersTotal());
        for (OrdersDetail ordersDetail :
                ordersDetails) {
            System.out.printf("%s--%.2f--%d\n",ordersDetail.getBookName(),ordersDetail.getBookPrice(),ordersDetail.getNumber());
        }
    }

}
